#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ======================================#
# @Author   : duanqizhong
# @time     : 2020/6/20 17:22
# @file     : common.py
# @Software : PyCharm
# ======================================#

from flask import request, Blueprint, jsonify, make_response

from apps.decorate.api_decorate import decorate_query, decorate_update, decorate_delete, decorate_insert, \
    decorate_session
from apps.models.table_crud import insert_table, delete_table, update_table, tables_redis_flushdb, \
    tables_redis_flush_tables, query_table

api_bp = Blueprint("api", __name__, url_prefix="/api/v1")


########## flush redis ##########
@api_bp.route("/redis/flushdb", methods=("GET",))
@decorate_query
@decorate_session
def redis_flushdb():
    secret_key = request.args.get("secret_key") if request.content_type is None else request.get_json().get(
        "params").get(
        "secret_key")
    if not secret_key:
        return {"status": False, "msg": "secret_key is required!"}
    infos = tables_redis_flushdb(secret_key)
    return make_response(jsonify(infos))


@api_bp.route("/redis/flush/tables", methods=("GET",))
def redis_flush_tables():
    infos = tables_redis_flush_tables()
    return make_response(jsonify(infos))


########## insert delete update query ##########
@api_bp.route("/table/<string:table>/add", methods=("POST",))
@decorate_insert
def api_insert_data(table):
    infos = insert_table(table, request.get_json())
    return make_response(jsonify(infos))


@api_bp.route("/table/<string:table>/delete/<int:num>", methods=("DELETE",))
@decorate_delete
def api_delete_data(table, num):
    infos = delete_table(table, num)
    return make_response(jsonify(infos))


@api_bp.route("/table/<string:table>/update", methods=("PUT",))
@decorate_update
def api_update_data(table):
    infos = update_table(table, request.get_json())
    return make_response(jsonify(infos))


@api_bp.route("/table/<string:table>", methods=("GET",))
@decorate_query
def api_query_data(table):
    params = request.args if request.content_type is None else request.get_json()
    infos = query_table(table, params)
    # log.info(infos)
    return make_response(jsonify(infos))

# ########## delete table ##########
# @api_bp.route("/app/delete", methods=("DELETE",))
# @decorate_delete
# def delete_app():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("app", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/app_var/delete", methods=("DELETE",))
# @decorate_delete
# def delete_app_var():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("app_var", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/business/delete", methods=("DELETE",))
# @decorate_delete
# def delete_business():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("business", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/constants/delete", methods=("DELETE",))
# @decorate_delete
# def delete_constants():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("constants", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/db_instance/delete", methods=("DELETE",))
# @decorate_delete
# def delete_db_instance():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("db_instance", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/env/delete", methods=("DELETE",))
# @decorate_delete
# def delete_env():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("env", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/env_app_host/delete", methods=("DELETE",))
# @decorate_delete
# def delete_env_app_host():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("env_app_host", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/env_business_var/delete", methods=("DELETE",))
# @decorate_delete
# def delete_env_business_var():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("env_business_var", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/hosts/delete", methods=("DELETE",))
# @decorate_delete
# def delete_hosts():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("hosts", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/operator_log/delete", methods=("DELETE",))
# @decorate_delete
# def delete_operator_log():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("operator_log", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/product/delete", methods=("DELETE",))
# @decorate_delete
# def delete_product():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("product", id)
#     return jsonify(infos)
#
# @api_bp.route("/requirements/delete", methods=("DELETE",))
# @decorate_delete
# def delete_requirements():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("requirements", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/requirement_interface/delete", methods=("DELETE",))
# @decorate_delete
# def delete_requirement_interface():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("requirement_interface", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/user/delete", methods=("DELETE",))
# @decorate_delete
# def delete_user():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("user", id)
#     return jsonify(infos)
#
#
# @api_bp.route("/vars/delete", methods=("DELETE",))
# @decorate_delete
# def delete_vars():
#     id = request.args.get("id") if request.content_type is None else request.get_json().get("params").get("id")
#     infos = delete_table("vars", id)
#     return jsonify(infos)
#
#
# ########## update table ##########
# @api_bp.route("/app/update", methods=("PUT",))
# @decorate_update
# def update_app():
#     infos = update_table("app", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/app_var/update", methods=("PUT",))
# @decorate_update
# def update_app_var():
#     infos = update_table("app_var", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/business/update", methods=("PUT",))
# @decorate_update
# def update_business():
#     infos = update_table("business", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/constants/update", methods=("PUT",))
# @decorate_update
# def update_constants():
#     infos = update_table("constants", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/db_instance/update", methods=("PUT",))
# @decorate_update
# def update_db_instance():
#     infos = update_table("db_instance", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/env/update", methods=("PUT",))
# @decorate_update
# def update_env():
#     infos = update_table("env", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/env_app_host/update", methods=("PUT",))
# @decorate_update
# def update_env_app_host():
#     infos = update_table("env_app_host", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/env_business_var/update", methods=("PUT",))
# @decorate_update
# def update_env_business_var():
#     infos = update_table("env_business_var", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/hosts/update", methods=("PUT",))
# @decorate_update
# def update_hosts():
#     infos = update_table("hosts", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/operator_log/update", methods=("PUT",))
# @decorate_update
# def update_operator_log():
#     infos = update_table("operator_log", request.get_json().get("params"))
#     return jsonify(infos)
#
# @api_bp.route("/product/update", methods=("PUT",))
# @decorate_update
# def update_product():
#     infos = update_table("product", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/requirements/update", methods=("PUT",))
# @decorate_update
# def update_requirements():
#     infos = update_table("requirements", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/requirement_interface/update", methods=("PUT",))
# @decorate_update
# def update_requirement_interface():
#     infos = update_table("requirement_interface", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/user/update", methods=("PUT",))
# @decorate_update
# def update_user():
#     infos = update_table("user", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/vars/update", methods=("PUT",))
# @decorate_update
# def update_vars():
#     infos = update_table("vars", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# ########## insert table ##########
# @api_bp.route("/app/add", methods=("POST",))
# @decorate_insert
# def insert_app():
#     infos = insert_table("app", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/app_var/add", methods=("POST",))
# @decorate_insert
# def insert_app_var():
#     infos = insert_table("app_var", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/business/add", methods=("POST",))
# @decorate_insert
# def insert_business():
#     infos = insert_table("business", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/constants/add", methods=("POST",))
# @decorate_insert
# def insert_constants():
#     infos = insert_table("constants", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/db_instance/add", methods=("POST",))
# @decorate_insert
# def insert_db_instance():
#     infos = insert_table("db_instance", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/env/add", methods=("POST",))
# @decorate_insert
# def insert_env():
#     infos = insert_table("env", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/env_app_host/add", methods=("POST",))
# @decorate_insert
# def insert_env_app_host():
#     infos = insert_table("env_app_host", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/env_business_var/add", methods=("POST",))
# @decorate_insert
# def insert_env_business_var():
#     infos = insert_table("env_business_var", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/hosts/add", methods=("POST",))
# @decorate_insert
# def insert_hosts():
#     infos = insert_table("hosts", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/operator_log/add", methods=("POST",))
# @decorate_insert
# def insert_operator_log():
#     infos = insert_table("operator_log", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/product/add", methods=("POST",))
# @decorate_insert
# def insert_product():
#     infos = insert_table("product", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/requirements/add", methods=("POST",))
# @decorate_insert
# def insert_requirements():
#     infos = insert_table("requirements", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/requirement_interface/add", methods=("POST",))
# @decorate_insert
# def insert_requirement_interface():
#     infos = insert_table("requirement_interface", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/user/add", methods=("POST",))
# @decorate_insert
# def insert_user():
#     infos = insert_table("user", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# @api_bp.route("/vars/add", methods=("POST",))
# @decorate_insert
# def insert_vars():
#     infos = insert_table("vars", request.get_json().get("params"))
#     return jsonify(infos)
#
#
# ########## query table ##########
# @api_bp.route("/app", methods=("GET",))
# @decorate_query
# def query_app():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("app", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/app_var", methods=("GET",))
# @decorate_query
# def query_app_var():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("app_var", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/business", methods=("GET",))
# @decorate_query
# def query_business():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("business", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/constants", methods=("GET",))
# @decorate_query
# def query_constants():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("constants", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/db_instance", methods=("GET",))
# @decorate_query
# def query_db_instance():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("db_instance", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/env", methods=("GET",))
# @decorate_query
# def query_env():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("env", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/env_app_host", methods=("GET",))
# @decorate_query
# def query_env_app_host():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("env_app_host", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/env_business_var", methods=("GET",))
# @decorate_query
# def query_env_business_var():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("env_business_var", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/hosts", methods=("GET",))
# @decorate_query
# def query_hosts():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("hosts", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/operator_log", methods=("GET",))
# @decorate_query
# def query_operator_log():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("operator_log", params)
#     return jsonify(infos)
#
# @api_bp.route("/product", methods=("GET",))
# @decorate_query
# def query_product():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("product", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/requirements", methods=("GET",))
# @decorate_query
# def query_requirements():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("requirements", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/requirement_interface", methods=("GET",))
# @decorate_query
# def query_requirement_interface():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("requirement_interface", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/user", methods=("GET",))
# @decorate_query
# def query_user():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("user", params)
#     return jsonify(infos)
#
#
# @api_bp.route("/vars", methods=("GET",))
# @decorate_query
# def query_vars():
#     params = request.args if request.content_type is None else request.get_json().get("params")
#     infos = query_table("vars", params)
#     return jsonify(infos)
########## next ##########
